/******************
 功能：大数据埋点组件
 write by : wuwiewei(邬畏畏)
 department : 用友 畅捷通 微企云
 www.github.com/flybirdsoft
 www.flybirdsoft.com/WUI
 www.cnblogs.com/wsoft

 ******************/
var shunter = {
    container:null,
    config : function(options){
        this.options = options;
    },

    on : function(options){
        /*
        options={
            queryString:{
                pixels:分辨率,
                refer : document.referer
                key:value,
                element:[
                    id,attr,class
                ],
            },
            isConsole:false|true,  是否控制台输出信息,可选项,默认false
            src : ""      大数据资源地址
            type:"click", 事件类型
            container : DOM, 参数可选
            events:[
                {
                    src : "",
                    element:DOM,   参数可选
                    type:"click",
                    handler:function(param){},
                    onerror:function(e){}
                }
            ]
        }
        */
        if(this.options==undefined)
        {
            this.options = options;
        }
        this.container = document.getElementsByTagName("body")[0];
        if(this.options.container!=undefined)
        {
            this.container = this.options.container;
        }
        this.bindEvent();
    },
    bindEvent : function(){
        var th = this;
        var container = this.container;
        var i,len;
        var events = this.options.events;
        var queryString = this.options.queryString;
        len = events==undefined?0:events.length;
        
        if(queryString!=undefined)
        {
            if(addEventListener)
            {
                container.addEventListener(this.options.type,function(i){
                    return function(e){
                        e.target = e.target || e.srcElement;
                        th.convertQueryString(queryString,e.target);
                        th.loadImage();
                    }
                }(i),false);
            }
            else
            {
                container.attachEvent("on"+events[i].type,function(i){
                    return function(){
                        var evt = window.event;
                        evt.target = evt.srcElement || src.target;
                        th.convertQueryString(queryString,e.target);
                        th.loadImage();
                    }

                }(i));
            }
        }

        /*options 里 含有 events*/
        if(len==0)
        {
            return;
        }

        if(addEventListener)
        {
            for(i=0;i<len;i++)
            {
                if(events[i].element==undefined)
                {
                    container = this.container;
                }
                else
                {
                    container = events[i].element;
                }
                if(this.options.type!=undefined&&events[i].type==undefined)
                {
                    events[i].type = this.options.type;
                }
                container.addEventListener(events[i].type,function(i){
                    return function(e){
                        e.target = e.target || e.srcElement;
                        th.convertQueryString(queryString,e.target);
                        th.loadImage(events[i]);
                        if(events[i].handler!=undefined)
                        {
                            events[i].handler.call(th,e);
                        }
                    }
                }(i),false);
            }
        }
        else
        {
            for(i=0;i<len;i++)
            {
                if(events[i].element==undefined)
                {
                    container = this.container;
                }
                else
                {
                    container = events[i].element;
                }
                if(this.options.type!=undefined&&events[i].type==undefined)
                {
                    events[i].type = this.options.type;
                }
                container.attachEvent("on"+events[i].type,function(i){
                    return function(){
                        var evt = window.event;
                        evt.target = evt.srcElement || evt.target;
                        th.convertQueryString(queryString,e.target);
                        th.loadImage(events[i]);
                        if(events[i].handler!=undefined)
                        {
                            events[i].handler.call(th,e);
                        }
                    }

                }(i));
            }
        }
    },

    start : function(){
        this.on();
    },

    loadImage : function(eventOptions){
        var th = this;
        var img,src;
        if(eventOptions==undefined || eventOptions.src==undefined)
        {
            src = this.options.src;
        }
        else
        {
            src = eventOptions.src;
        }
        img = new Image();
        img.onerror = function(){
            if(eventOptions!=undefined && eventOptions.onerror!=undefined)
            {
                eventOptions.onerror.call(th);
            }
        }
        img.src = src + this.queryString;
        this.debuger("src : "+img.src);
    },
    convertQueryString : function(queryStringOptions,dom){
        var v,i,len;
        var queryString = queryStringOptions;
        var attr;
        this.queryString = "";

        for(v in queryString)
        {
            if(v!="element")
            {
                this.queryString += "&" + v + "=" + queryString[v];
            }
            else
            {
                len = queryString[v].length;
                for(i=0;i<len;i++)
                {
                    attr = dom.getAttribute(queryString[v][i]);
                    if(attr==null)
                    {
                        attr = "";
                    }
                    this.queryString += "&" + queryString[v][i] + "=" + attr;
                }
            }
        }
        this.queryString += "&refer=" + document.referrer;
        this.debuger("queryString : "+this.queryString);
        return this.queryString;
    },
    debuger : function(info){
        if(this.options.isConsole!=undefined&&this.options.isConsole)
        {
            if(console!=undefined)
            {
                console.log(info);
            }
        }
    }
};




/*
调用方式一：
shunter.on({
    events:[
        {
            type:"click",
            element : document.getElementById("d"),
        },
        {
            
            type:"click",
            element : document.getElementById("d"),
            handler:function(e){
                alert(e.target+"222");
            }
        }   
    ]
});
*/

/*
var config = {
    src : "http://www.so.com/?",
    type : "click",
    isConsole:true,
    queryString:{
        "pixels":"1900*1024",
        "key":"value",
        "element":[
            "id","class"
        ]
    }
};
shunter.config(config);

shunter.start();
*/

